/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.cspimp.esrz.service.impl;

import me.zhengjie.modules.cspimp.esrz.domain.EsrzHiddenRiskLedgerManage;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.cspimp.esrz.repository.EsrzHiddenRiskLedgerManageRepository;
import me.zhengjie.modules.cspimp.esrz.service.EsrzHiddenRiskLedgerManageService;
import me.zhengjie.modules.cspimp.esrz.service.dto.EsrzHiddenRiskLedgerManageDto;
import me.zhengjie.modules.cspimp.esrz.service.dto.EsrzHiddenRiskLedgerManageQueryCriteria;
import me.zhengjie.modules.cspimp.esrz.service.mapstruct.EsrzHiddenRiskLedgerManageMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://docs.auauz.net
* @description 服务实现
* @author 黄星星
* @date 2020-05-30
**/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class EsrzHiddenRiskLedgerManageServiceImpl implements EsrzHiddenRiskLedgerManageService {

    private final EsrzHiddenRiskLedgerManageRepository esrzHiddenRiskLedgerManageRepository;
    private final EsrzHiddenRiskLedgerManageMapper esrzHiddenRiskLedgerManageMapper;

    @Override
    public Map<String,Object> queryAll(EsrzHiddenRiskLedgerManageQueryCriteria criteria, Pageable pageable){
        Page<EsrzHiddenRiskLedgerManage> page = esrzHiddenRiskLedgerManageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(esrzHiddenRiskLedgerManageMapper::toDto));
    }

    @Override
    public List<EsrzHiddenRiskLedgerManageDto> queryAll(EsrzHiddenRiskLedgerManageQueryCriteria criteria){
        return esrzHiddenRiskLedgerManageMapper.toDto(esrzHiddenRiskLedgerManageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public EsrzHiddenRiskLedgerManageDto findById(Long id) {
        EsrzHiddenRiskLedgerManage esrzHiddenRiskLedgerManage = esrzHiddenRiskLedgerManageRepository.findById(id).orElseGet(EsrzHiddenRiskLedgerManage::new);
        ValidationUtil.isNull(esrzHiddenRiskLedgerManage.getId(),"EsrzHiddenRiskLedgerManage","id",id);
        return esrzHiddenRiskLedgerManageMapper.toDto(esrzHiddenRiskLedgerManage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public EsrzHiddenRiskLedgerManageDto create(EsrzHiddenRiskLedgerManage resources) {
        return esrzHiddenRiskLedgerManageMapper.toDto(esrzHiddenRiskLedgerManageRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(EsrzHiddenRiskLedgerManage resources) {
        EsrzHiddenRiskLedgerManage esrzHiddenRiskLedgerManage = esrzHiddenRiskLedgerManageRepository.findById(resources.getId()).orElseGet(EsrzHiddenRiskLedgerManage::new);
        ValidationUtil.isNull( esrzHiddenRiskLedgerManage.getId(),"EsrzHiddenRiskLedgerManage","id",resources.getId());
        esrzHiddenRiskLedgerManage.copy(resources);
        esrzHiddenRiskLedgerManageRepository.save(esrzHiddenRiskLedgerManage);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            esrzHiddenRiskLedgerManageRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<EsrzHiddenRiskLedgerManageDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (EsrzHiddenRiskLedgerManageDto esrzHiddenRiskLedgerManage : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("创建时间", esrzHiddenRiskLedgerManage.getCreateTime());
            map.put("更新时间", esrzHiddenRiskLedgerManage.getUpdateTime());
            map.put("创建者", esrzHiddenRiskLedgerManage.getCreateBy());
            map.put("更新者", esrzHiddenRiskLedgerManage.getUpdateBy());
            map.put("部门主键", esrzHiddenRiskLedgerManage.getDepId());
            map.put("部门名称", esrzHiddenRiskLedgerManage.getDepName());
            map.put("隐患内容", esrzHiddenRiskLedgerManage.getHiddenRiskContent());
            map.put("风险等级", esrzHiddenRiskLedgerManage.getRiskLevel());
            map.put("更改要求", esrzHiddenRiskLedgerManage.getChangeRequirement());
            map.put("整改时限", esrzHiddenRiskLedgerManage.getRectificationTimeLimit());
            map.put("临时防范措施", esrzHiddenRiskLedgerManage.getInterimPrecautions());
            map.put("整改责任人", esrzHiddenRiskLedgerManage.getRectifierId());
            map.put("整改责任人姓名", esrzHiddenRiskLedgerManage.getRectifierName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}